package cn.edu.scujcc.customofsichuan;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.ui.StyledPlayerView;


public class video extends AppCompatActivity{
    private ExoPlayer player;
    private ProgressBar progressBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live);
        StyledPlayerView exoplayerView = findViewById(R.id.exoplayer);
        progressBar = findViewById(R.id.progressBar);

        Toolbar toolbar = findViewById(R.id.toolbar_video);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initExoplayer(exoplayerView);




    }

    private void initExoplayer(StyledPlayerView exoplayerView) {


        player = new ExoPlayer.Builder(this).build();
        exoplayerView.setPlayer(player);

        MediaItem mediaItem1 = MediaItem.fromUri("https://outin-2b239399477911ed91ce00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/sv/1ae8ab08-183bb1e2ed0/1ae8ab08-183bb1e2ed0.mp4?Expires=1666001916&OSSAccessKeyId=LTAI3DkxtsbUyNYV&Signature=mgS49VzLHTHOr%2FSO0Rs%2FaXXle%2FY%3D");
        MediaItem mediaItem2 = MediaItem.fromUri("https://outin-2b239399477911ed91ce00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/sv/57773f13-183bb1c9c7f/57773f13-183bb1c9c7f.mp4?Expires=1666001979&OSSAccessKeyId=LTAI3DkxtsbUyNYV&Signature=BpXXgLY9nTOn80p1dqcqb4CxAsw%3D");
        MediaItem mediaItem3 = MediaItem.fromUri("https://outin-2b239399477911ed91ce00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/sv/149679e-183bb1a88e0/149679e-183bb1a88e0.mp4?Expires=1666002014&OSSAccessKeyId=LTAI3DkxtsbUyNYV&Signature=yZgitBgoG0MBHXwKGYNSWEksxVc%3D");
        MediaItem mediaItem4 = MediaItem.fromUri("https://outin-2b239399477911ed91ce00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/sv/4bcf92e4-183bb1a896e/4bcf92e4-183bb1a896e.mp4?Expires=1666002039&OSSAccessKeyId=LTAI3DkxtsbUyNYV&Signature=Pd803AHtLtYcfUalzviwi64pe8A%3D");
        MediaItem mediaItem5 = MediaItem.fromUri("https://outin-2b239399477911ed91ce00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/sv/57773f13-183bb1c9c7f/57773f13-183bb1c9c7f.mp4?Expires=1665392118&OSSAccessKeyId=LTAI3DkxtsbUyNYV&Signature=bu%2B%2FAcqIsSXsroD1SGrdU8e93Rg%3D");
        MediaItem mediaItem6 = MediaItem.fromUri("https://outin-2b239399477911ed91ce00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/sv/382acda9-183bb1bfb71/382acda9-183bb1bfb71.mp4?Expires=1665392149&OSSAccessKeyId=LTAI3DkxtsbUyNYV&Signature=N77XUK%2BW7bpALujHp%2FEsX3g99%2Fw%3D");
        MediaItem mediaItem7 = MediaItem.fromUri("https://outin-2b239399477911ed91ce00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/sv/61a31ec5-183bb1b663d/61a31ec5-183bb1b663d.mp4?Expires=1665392213&OSSAccessKeyId=LTAI3DkxtsbUyNYV&Signature=hfX8gDDC7hv21FxfjwykaOpSL98%3D");
        MediaItem mediaItem8 = MediaItem.fromUri("https://outin-2b239399477911ed91ce00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/sv/4bcf92e4-183bb1a896e/4bcf92e4-183bb1a896e.mp4?Expires=1665331917&OSSAccessKeyId=LTAI3DkxtsbUyNYV&Signature=rOy85N5pi7oXDbyn2sJrFhf0dnc%3D");
        MediaItem mediaItem9 = MediaItem.fromUri("https://outin-2b239399477911ed91ce00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/sv/39ee33d9-183bb1a8973/39ee33d9-183bb1a8973.mp4?Expires=1665392252&OSSAccessKeyId=LTAI3DkxtsbUyNYV&Signature=XHTlg4fhet1VYdyX%2F%2FNez3hPql0%3D");


        //将视频添加到播放列表
        player.addMediaItem(mediaItem1);
        player.addMediaItem(mediaItem2);
        player.addMediaItem(mediaItem3);
        player.addMediaItem(mediaItem4);
        player.addMediaItem(mediaItem5);
        player.addMediaItem(mediaItem6);
        player.addMediaItem(mediaItem7);
        player.addMediaItem(mediaItem8);
        player.addMediaItem(mediaItem9);

        //循环播放
        player.setRepeatMode(Player.REPEAT_MODE_ONE);
        //增加监视器
        player.addListener(new Player.Listener() {
            @Override
            public void onPlaybackStateChanged(int playbackState) {
                switch (playbackState){
                    //加载缓存，且视频未准备好时触发
                    case Player.STATE_BUFFERING:
                        progressBar.setVisibility(View.VISIBLE);
                        break;
                    //视频准备好时触发
                    case Player.STATE_READY:
                        progressBar.setVisibility(View.GONE);
                        break;
                    //视频结束时触发
                    case Player.STATE_ENDED:
                        if (!player.hasNextMediaItem()){
                            Toast.makeText(video.this,"已经是最后一个视频啦！",Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            }
        });

        player.prepare();
        player.play();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        player.release();

    }

}